const DB = require('../model/db.js');

const {
    GraphQLObjectType,
    GraphQLString,
    GraphQLInt,
    GraphQLList,
    GraphQLSchema,
    GraphQLNonNull
} = require('graphql')

//1、定义导航的schema

var NavSchema = new GraphQLObjectType({
    name: 'nav',
    fields: {
        _id: {
            type: GraphQLString
        },
        name: {
            type: GraphQLString
        },
        description: {
            type: GraphQLString,
        },
        crtTime: {
            type: GraphQLString
        },
        count: {
            type: GraphQLInt,
            async resolve(parent, args) {
                const result = await DB.count('cats');
                return result;
            }
        }
    }
})

// 定义文章分类的schema

const ArticleCateSchema = new GraphQLObjectType({
    name: 'articlecate',
    fields: {
        _id: {
            type: GraphQLString,
        },
        title: {
            type: GraphQLString,
        },
        description: {
            type: GraphQLString
        },
        keywords: {
            type: GraphQLString,
        },
        pid: {
            type: GraphQLInt
        },
        add_time: {
            type: GraphQLString
        },
        status: {
            type: GraphQLString
        }
    }
});

// 定义文章的Schema
const ArticleShema = new GraphQLObjectType({
    name: 'articleShema',
    fields: {}
})

//2、定义一个根 配置调用Schema的方法

var RootSchema = new GraphQLObjectType({
    name: 'root',
    fields: {
        navList: {
            type: GraphQLList(NavSchema),
            args: {
                page: {
                    type: GraphQLInt
                },
                pageSize: {
                    type: GraphQLInt
                }
            },
            async resolve(parent, args) {
                const page = args.page || 1;
                const pageSize = args.pageSize || 10;
                const navList = await DB.find('cats', {}, {}, {
                    page,
                    pageSize,
                    sort: {"crtTime": -1},
                });
                return navList;
            }
        },
        oneNavList: {
            type: NavSchema,
            args: {
                _id: {
                    type: GraphQLString
                },
            },
            async resolve(parent, args) {
                const oneNavList = await DB.find('cats', {'_id': DB.getObjectId(args._id)});
                return oneNavList[0];
            }
        },
        articleCateList: {
            type: GraphQLList(ArticleCateSchema),
            args: {},
            async resolve(parent, args) {
                const articleCateList = await DB.find('articlecate', {});
                return articleCateList;
            }
        }
    }
})

// 定义根MutationRoot实现增删改

const MutationRoot = new GraphQLObjectType({
    name: 'Mutation',
    fields: {
        addNav: {
            type: NavSchema,
            args: {
                name: {
                    type: new GraphQLNonNull(GraphQLString),
                },
                description: {
                    type: new GraphQLNonNull(GraphQLString),
                },
            },
            async resolve(parent, args) {
                const crtTime = Date.now();
                const result = await DB.insert('cats', {
                    name: args.name,
                    description: args.description,
                    crtTime:String(crtTime)
                });
                console.log(result.ops, '增加数据');
                return result.ops[0]
            }
        }
    }
});

//3、把查询的根 挂载到GraphQLSchema


module.exports = new GraphQLSchema({
    query: RootSchema,
    mutation: MutationRoot,
})
